/**
 * 
 */
package com.jolestar.yayaweibotong.task;

import java.util.List;
import java.util.logging.Level;

import com.google.appengine.api.labs.taskqueue.Queue;
import com.google.appengine.api.labs.taskqueue.QueueFactory;
import com.google.appengine.api.labs.taskqueue.TaskOptions;
import com.jolestar.yayaweibotong.YayaException;

/**
 * @author jolestar@gmail.com
 *
 */
public abstract class QueueCronJob extends CronJob {

	private String queneName;
	public QueueCronJob(String jobName,String queneName) {
		super(jobName);
		this.queneName = queneName;
	}

	/* (non-Javadoc)
	 * @see com.jolestar.yayaweibotong.task.CronJob#doRealJob()
	 */
	@Override
	protected void doRealJob() {
		Queue queue = QueueFactory.getQueue(this.queneName);
		if(queue == null){
			throw new YayaException("can not find queue by name:"+this.jobName);
		}
		List<TaskOptions> tasks = this.buildTasks();
		for(TaskOptions task:tasks){
			try{
				queue.add(task);
			}catch(Exception e){
				log.severe("add task error "+task.getUrl());
				log.log(Level.SEVERE,e.getMessage(),e);
			}
		}
		log.info("add "+tasks.size()+" task to queue:"+this.jobName);
	}
	
	protected abstract List<TaskOptions> buildTasks();
	

}
